package com.syqc.login

import android.content.Context
import android.content.Intent
import com.syqc.comlib.MenuKey
import com.syqc.comlib.UserConstant
import com.syqc.comlib.utils.CacheMMKV
import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.FunInfo
import com.syqc.entity.GpsDetail
import com.syqc.entity.Menu
import com.syqc.utils.Keys
import java.util.stream.Collectors

//


object FunKey {
    const val FUN_Talk = "FunTalk"
    const val FUN_NAVI = "FunNavi"
    const val FUN_CALL = "FunCall"
}

/**
 * 菜单过滤类型
 *
 * @constructor Create empty Fun type
 */
enum class FunType {
    CAR_MAP, CAS, POPUP, SINGLE, SIMPLE
}

object InfoSetModel {
    private const val infosKey = "InfoKey"

    //全部菜单列表
    private val infos = mutableListOf<FunInfo>()

    //用户设置列表
    private val hadInfos = mutableListOf<FunInfo>()

    //车辆设置列表
    private val hadCarInfos = mutableListOf<FunInfo>()

    //其它key
    private val otherKeys = mutableListOf<String>()

    private val allMaps = HashMap<String, FunInfo>()

    init {
        initMasp()
        initCarInfo()
    }


    private fun initMasp() {
        allMaps.clear()
        allMaps[MenuKey.MENU_106] = FunInfo(MenuKey.MENU_106, false)
        allMaps[FunKey.FUN_Talk] = FunInfo(FunKey.FUN_Talk, false)
        allMaps[MenuKey.MENU_104] = FunInfo(MenuKey.MENU_104, false)
        allMaps[MenuKey.MENU_917] = FunInfo(MenuKey.MENU_917, false)
        allMaps[MenuKey.MENU_229] = FunInfo(MenuKey.MENU_229, false)
        allMaps[MenuKey.MENU_201] = FunInfo(MenuKey.MENU_201, false)
        allMaps[MenuKey.MENU_912] = FunInfo(MenuKey.MENU_912, false)
        allMaps[MenuKey.MENU_916] = FunInfo(MenuKey.MENU_916, false)
        allMaps[MenuKey.MENU_103] = FunInfo(MenuKey.MENU_103, false)
        allMaps[MenuKey.MENU_204] = FunInfo(MenuKey.MENU_204, false)
        allMaps[MenuKey.MENU_902] = FunInfo(MenuKey.MENU_902, false)
        allMaps[MenuKey.MENU_907] = FunInfo(MenuKey.MENU_907, false)
        allMaps[MenuKey.MENU_908] = FunInfo(MenuKey.MENU_908, false)
        allMaps[MenuKey.MENU_903] = FunInfo(MenuKey.MENU_903, false)
        allMaps[FunKey.FUN_NAVI] = FunInfo(FunKey.FUN_NAVI, false)
        allMaps[FunKey.FUN_CALL] = FunInfo(FunKey.FUN_CALL, false)
    }

    private var isFirst = CacheMMKV.decodeBooleanSave("info_first", true)

    /**
     * 初次初始化
     */
    private fun initInfos() {
        if (isFirst) {
            isFirst = false
            CacheMMKV.encodeSave("info_first", false)
            allMaps[MenuKey.MENU_204]?.isChoose = true
            allMaps[MenuKey.MENU_916]?.isChoose = true
            allMaps[FunKey.FUN_Talk]?.isChoose = true
            allMaps[FunKey.FUN_NAVI]?.isChoose = true
            allMaps[FunKey.FUN_CALL]?.isChoose = true
            CacheMMKV.encodeSave(infosKey, GsonUtil.toJson(allMaps.values))
        }
        infos.addAll(allMaps.values)
    }


    /**
     * 判断是否有权限
     *
     * @param menuIds
     */
    fun isHad(menuIds: MutableList<String>?) {
        infos.clear()
        allMaps.values.forEach {
            it.isChoose = false
        }

        val infoStr = CacheMMKV.decodeStringSave(infosKey)
        if (infoStr.isEmpty()) {
            initInfos()
        } else {
            handleChoose(infoStr)
        }

        val newList=infos.stream().distinct().collect(Collectors.toList())
        infos.clear()
        infos.addAll(newList)

        hadInfos.clear()
        infos.forEach {
            val key = it.key
            if ((key == FunKey.FUN_Talk && menuIds?.contains(MenuKey.MENU_106) == true)
                || key == FunKey.FUN_NAVI
                || key == FunKey.FUN_CALL
                || menuIds?.contains(key) == true
            ) hadInfos.add(it)
        }
        otherKeys.forEach {
            if ((it == FunKey.FUN_Talk && menuIds?.contains(MenuKey.MENU_106) == true) || it == FunKey.FUN_CALL || it == FunKey.FUN_NAVI) {
                hadInfos.add(allMaps[it]!!)
            } else if (menuIds?.contains(it) == true) hadInfos.add(allMaps[it]!!)
        }

        chooseNum = hadInfos.filter { it.isChoose }.size
        CacheMMKV.encodeSave(infosKey, GsonUtil.toJson(hadInfos))
    }

    /**
     * 处理纯定位模式数据
     *
     */
    fun handleLocation() {
        val list = ArrayList<FunInfo>()
        hadInfos.forEach {
            if (it.key != FunKey.FUN_Talk && it.key != MenuKey.MENU_106) list.add(it)
        }
        hadInfos.clear()
        hadInfos.addAll(list)
        chooseNum = hadInfos.filter { it.isChoose }.size
        CacheMMKV.encodeSave(infosKey, GsonUtil.toJson(hadInfos))
    }

    private fun handleChoose(infoStr: String) {
        val data = if (infoStr.contains("a") && infoStr.contains("c") && infoStr.contains("d")) {
            val menu = GsonUtil.formArrayList(infoStr, Menu::class.java)
            val array = ArrayList<FunInfo>()
            menu.forEach {
                array.add(FunInfo(it.a, it.c, it.d))
            }
            array
        } else {
            GsonUtil.formArrayList(infoStr, FunInfo::class.java)
        }
        val allKeys = allMaps.keys
        otherKeys.clear()
        allKeys.forEach {
            if (!isHadByKey(it, data)) otherKeys.add(it)
        }
        infos.addAll(data)
    }

    /**
     * 是否包含这个key
     *
     * @param key
     * @param data
     * @return
     */
    private fun isHadByKey(key: String, data: MutableList<FunInfo>): Boolean {
        for (info in data) {
            if (info.key == key) return true
        }
        return false
    }

    fun saveChoose(data: MutableList<FunInfo>) {
        hadInfos.clear()
        hadInfos.addAll(data)
        chooseNum = hadInfos.filter { it.isChoose }.size
        CacheMMKV.encodeSave(infosKey, GsonUtil.toJson(hadInfos))
    }

    ///////////////////////////////////Cars///////////////////////////////////////////////////////
    private fun initCarInfo() {
        if (hadCarInfos.isEmpty()) {
            hadCarInfos.add(FunInfo(MenuKey.MENU_204, true))
            hadCarInfos.add(FunInfo(FunKey.FUN_Talk, true))
            hadCarInfos.add(FunInfo(MenuKey.MENU_916, true))
            hadCarInfos.add(FunInfo(FunKey.FUN_NAVI, true))
            hadCarInfos.add(FunInfo(FunKey.FUN_CALL, true))
            hadCarInfos.add(FunInfo(MenuKey.MENU_103, true))
        }
    }

    /**
     * 获取车辆信息
     *
     */
    fun getCarInfo() = hadCarInfos


    //获取选中的菜单
    fun getChooseList(type: FunType): MutableList<FunInfo> {
        val list = if (UserConstant.isLocationMode) {
            hadInfos.filter {
                it.isChoose && it.key != FunKey.FUN_Talk && it.key != MenuKey.MENU_106
            }
        } else {
            hadInfos.filter { it.isChoose }
        }

        val filter = when (type) {
            FunType.CAR_MAP -> {
                list.forEach {
                    it.isCheckable = !(it.key == MenuKey.MENU_106 || it.key == MenuKey.MENU_104)
                }
                list
            }

            FunType.POPUP -> {
                list.forEach {
                    it.isCheckable = !(it.key == FunKey.FUN_Talk || it.key == MenuKey.MENU_106)
                }
                list
            }

            FunType.SINGLE -> {
                list.forEach {
                    it.isCheckable = it.key != MenuKey.MENU_103
                }
                list
            }

            else -> {
                list.forEach {
                    it.isCheckable = true
                }
                list
            }
        }

        return filter.toMutableList()
    }


    //选中的数量
    var chooseNum = 5


    //全部菜单
    fun getAllInfos() = hadInfos

    /**
     * 跳转到指定Activity
     *
     * @param context
     *
     * @param zClass
     * @param entity
     */
    fun startFunActivity(context: Context, zClass: Class<*>, entity: GpsDetail?) {
        context.startActivity(
            Intent(context, zClass).putExtra(Keys.INTENT_CAR_ID, entity?.carId)
                .putExtra(Keys.INTENT_PLATE, entity?.carPlate)
                .putExtra(Keys.INTENT_CAR_NAME, entity?.carName)
                .putExtra(Keys.INTENT_TEAM_NAME, entity?.teamName)
                .putExtra(Keys.VIDEO, entity?.videos)
        )
    }

    private val defaultKey =
        "${MenuKey.MENU_916},${MenuKey.MENU_204},${FunKey.FUN_Talk},${FunKey.FUN_CALL}"
    private val modifyKey =
        "${MenuKey.MENU_912},${MenuKey.MENU_907},${MenuKey.MENU_908},${MenuKey.MENU_902},${FunKey.FUN_Talk},${FunKey.FUN_CALL}"
    private val monitorKey =
        "${MenuKey.MENU_229},${MenuKey.MENU_917},${MenuKey.MENU_201},${MenuKey.MENU_104},${MenuKey.MENU_106},${FunKey.FUN_NAVI}"

    //修改模式
    fun changModel(model: InfoMode = InfoMode.DefaultMode): MutableList<FunInfo> {
        val keys =
            when (model) {
                InfoMode.DefaultMode -> {
                    defaultKey
                }

                InfoMode.ModifyMode -> {
                    modifyKey
                }

                InfoMode.MonitorMode -> {
                    monitorKey
                }
            }
        hadInfos.forEach {
            it.isChoose = keys.contains(it.key)
        }
        chooseNum = hadInfos.filter { it.isChoose }.size
        return hadInfos
    }

      fun isInfoSet(model: InfoMode = InfoMode.DefaultMode):Boolean {
        val chooseInfo = hadInfos.filter { it.isChoose }
        var length=0
        val keys =
            when (model) {
                InfoMode.DefaultMode -> {
                    length=4
                    defaultKey
                }

                InfoMode.ModifyMode -> {
                    length=6
                    modifyKey
                }

                InfoMode.MonitorMode -> {
                    length=6
                    monitorKey
                }
            }
        for (funInfo in chooseInfo) {
            if (!keys.contains(funInfo.key))return false
        }
        return chooseInfo.size==length
    }
}

sealed class InfoMode {
    data object DefaultMode : InfoMode()
    data object MonitorMode : InfoMode()
    data object ModifyMode : InfoMode()
}
